﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ScrumLite.Data.Tests
{
    /// <summary>
    /// Summary description for RoleStoreFixture
    /// </summary>
    [TestClass]
    public class RoleStoreFixture
    {
        IRoleStore store;
        static Guid projectId = new Guid("1C4F9968-8D5E-4d8a-9BD8-9A8DF1434B5A");

        static Guid r1Id = new Guid("1F4F9968-8D5E-4d8a-9BD8-9A8DF1434B5A");
        static Guid r2Id = new Guid("1F4F9968-8D5E-4d8a-9BD8-9A8DF1434B5B");
        static Guid r3Id = new Guid("1F4F9968-8D5E-4d8a-9BD8-9A8DF1434B5C");

        static Guid a1Id = new Guid("1F4F9968-8D5E-4d8a-9BD8-9A8DF1434B5D");
        static Guid a2Id = new Guid("1F4F9968-8D5E-4d8a-9BD8-9A8DF1434B5F");
        static Guid a3Id = new Guid("1F4F9968-8D5E-4d8a-9BD8-9A8DF1434B60");

        [TestInitialize]
        public void Initialize()
        {
            store = new RoleStore();
        }

        [TestMethod()]
        public void ShouldRetrieveARole()
        {
            using (TransactionScope scope = new TransactionScope())
            {
                CreateFakeRoleStore();

                Role actual = this.store.GetRole(r1Id);

                Assert.IsNotNull(actual);
                Assert.AreEqual(r1Id, actual.id);
            }
        }

        [TestMethod()]
        public void ShouldRetrieveProjectRoles()
        {
            using (TransactionScope scope = new TransactionScope())
            {
                CreateFakeRoleStore();

                Role[] actual = this.store.GetProjectRoles(projectId);
                Assert.AreEqual(3, actual.Length);

                Assert.IsNotNull(actual.FirstOrDefault(r => r.id == r1Id));
                Assert.IsNotNull(actual.FirstOrDefault(r => r.id == r2Id));
                Assert.IsNotNull(actual.FirstOrDefault(r => r.id == r3Id));
            }
        }

        [TestMethod()]
        public void ShouldRetrieveRolesAbilities()
        {
            using (TransactionScope scope = new TransactionScope())
            {
                CreateFakeRoleStore();

                Ability[] actual = this.store.GetRoleAbilities(projectId,r1Id);
                Assert.AreEqual(3, actual.Length);

                Assert.IsNotNull(actual.FirstOrDefault(a => a.id == a1Id));
                Assert.IsNotNull(actual.FirstOrDefault(a => a.id == a2Id));
                Assert.IsNotNull(actual.FirstOrDefault(a => a.id == a3Id));
            }
        }

        private static void CreateFakeRoleStore()
        {

            using (ScrumLiteDataContext context = new ScrumLiteDataContext())
            {
                Project project = new Project() { endsOn = DateTime.Today, id = projectId, name = "FakeProject", security_group = "", startsOn = DateTime.Today, uri_repository = "", user_ownedBy = "" };
                context.Projects.InsertOnSubmit(project);

                Role role1 = new Role() { id = r1Id, name = "Role 1" };
                context.Roles.InsertOnSubmit(role1);
                Role role2 = new Role() { id = r2Id, name = "Role 2" };
                context.Roles.InsertOnSubmit(role2);
                Role role3 = new Role() { id = r3Id, name = "Role 3" };
                context.Roles.InsertOnSubmit(role3);

                ProjectRole projectRole1 = new ProjectRole() { id = Guid.NewGuid(), fk_project = projectId, fk_role = r1Id };
                context.ProjectRoles.InsertOnSubmit(projectRole1);
                ProjectRole projectRole2 = new ProjectRole() { id = Guid.NewGuid(), fk_project = projectId, fk_role = r2Id };
                context.ProjectRoles.InsertOnSubmit(projectRole2);
                ProjectRole projectRole3 = new ProjectRole() { id = Guid.NewGuid(), fk_project = projectId, fk_role = r3Id };
                context.ProjectRoles.InsertOnSubmit(projectRole3);

                Ability ability1 = new Ability { id = a1Id, name = "Ability 1" };
                Ability ability2 = new Ability { id = a2Id, name = "Ability 2" };
                Ability ability3 = new Ability { id = a3Id, name = "Ability 3" };

                context.Abilities.InsertOnSubmit(ability1);
                context.Abilities.InsertOnSubmit(ability2);
                context.Abilities.InsertOnSubmit(ability3);

                RoleAbility rab1 = new RoleAbility() { id = Guid.NewGuid(), fk_role = r1Id, fk_ability = a1Id };
                RoleAbility rab2 = new RoleAbility() { id = Guid.NewGuid(), fk_role = r1Id, fk_ability = a2Id };
                RoleAbility rab3 = new RoleAbility() { id = Guid.NewGuid(), fk_role = r1Id, fk_ability = a3Id };

                context.RoleAbilities.InsertOnSubmit(rab1);
                context.RoleAbilities.InsertOnSubmit(rab2);
                context.RoleAbilities.InsertOnSubmit(rab3);

                context.SubmitChanges();
            }
        }
    }
}
